package com.woniuxy.mini;

import com.woniuxy.core.service.CouponService;
import com.woniuxy.core.service.UserCouponService;
import com.woniuxy.entity.Coupon;
import com.woniuxy.entity.DTO.UserCouponDTO;
import com.woniuxy.entity.UserCoupon;
import com.woniuxy.enums.ResponseEnum;
import com.woniuxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券管理控制器
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private UserCouponService userCouponService;

    @Autowired
    private CouponService couponService;

    /**
     * 新增优惠券
     * @param coupon 优惠券信息
     * @return 响应数据（是否成功）
     */
    @PostMapping("/add")
    public ResponseData<Boolean> addCoupon(@RequestBody Coupon coupon) {
        if (coupon.getCreateTime() == null) {
            coupon.setCreateTime(new java.util.Date());
        }

        // 如果未设置 endTime，则默认为某个值（例如：当前时间 + 7天）
        if (coupon.getEndTime() == null) {
            coupon.setEndTime(new java.util.Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L));
        }

        boolean result = couponService.save(coupon);
        return result ? ResponseData.ok(true) : ResponseData.fail(ResponseEnum.INTERFACE_ERR);
    }


    /**
     * 删除优惠券
     * @param id 优惠券ID
     * @return 响应数据（是否成功）
     */
    @DeleteMapping("/{id}")
    public ResponseData<Boolean> deleteCoupon(@PathVariable Integer id) {
        boolean result = couponService.removeById(id);
        return result ? ResponseData.ok(true) : ResponseData.fail(ResponseEnum.INTERFACE_ERR);
    }

    /**
     * 更新优惠券
     * @param coupon 优惠券信息
     * @return 响应数据（是否成功）
     */
    @PutMapping
    public ResponseData<Boolean> updateCoupon(@RequestBody Coupon coupon) {
        boolean result = couponService.updateById(coupon);
        return result ? ResponseData.ok(true) : ResponseData.fail(ResponseEnum.INTERFACE_ERR);
    }

    /**
     * 查询所有优惠券
     * @return 响应数据（优惠券列表）
     */
    @GetMapping("/all")
    public ResponseData<List<Coupon>> getAllCoupons() {
        List<Coupon> coupons = couponService.list();
        return ResponseData.ok(coupons);
    }

    /**
     * 根据ID查询优惠券
     * @param couponId 优惠券ID
     * @return 响应数据（优惠券详情）
     */
   /* @GetMapping("/{id}")
    public ResponseData<Coupon> getCouponById(@PathVariable Integer id) {
        Coupon coupon = couponService.getById(id);
        return coupon != null ? ResponseData.ok(coupon) : ResponseData.fail(ResponseEnum.INTERFACE_ERR);
    }*/
    @GetMapping("/{couponId}")
    public Coupon getCouponById(@PathVariable("couponId") Integer couponId) {
      return couponService.getById(couponId);

    }
/*
* 用户兑换优惠劵
*/
@PostMapping("/exchange")
public ResponseData<Boolean> exchangeCoupon(@RequestBody UserCoupon userCoupon) {
    boolean result = couponService.exchangeCoupon(userCoupon.getUserId(), userCoupon.getCouponId());
    return result ? ResponseData.ok(true) : ResponseData.fail(ResponseEnum.INTERFACE_ERR);
}
    @GetMapping("/user/{userId}")
    public ResponseData<List<UserCouponDTO>> getUserCoupons(@PathVariable("userId") Integer userId) {
        List<UserCouponDTO> userCoupons = userCouponService.getUserCouponDTOsByUserId(userId);
        return ResponseData.ok(userCoupons);
    }

}
